package cn.gson.springboot.controller.cuscontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.cusservice.CustomerService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/vue")
public class CustomerController {
    @Autowired
    CustomerService service;

    //    分页查询我的客户
    @RequestMapping("/page-select")
    @ResponseBody
    public Map<String, Object> pageSelect(Integer page, Integer size,Integer empId,Integer deptId, String cusName, Integer typeId) {
        System.out.println("客户分页："+page+","+size);
        Page<Object> obj = PageHelper.startPage(page, size);
        service.pageSelect(empId,deptId,cusName, typeId);
        //        查询客户类型
        List<CustomerType> customerTypes = service.selectCusType();
        List<Customer> exportList=service.pageSelect(empId,deptId,cusName, typeId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", obj.getTotal());//还没分页的数据，总条数
        map.put("rows", obj.getResult());//分页后的数据，一页展示多少数据
        map.put("exportList",exportList);
        map.put("typeList", customerTypes);
        return map;
    }

    //    查询我关注的客户
    @RequestMapping("/select-attention")
    @ResponseBody
    public Map<String,Object> selectAttention(Integer page,Integer size,Integer empId,Integer deptId, String cusName, Integer typeId){
        System.out.println("关注客户："+page+","+size+","+cusName+","+typeId);
        Page<Object> obj = PageHelper.startPage(page, size);
        service.selectAttention(empId,deptId,cusName,typeId);

        Map<String,Object> map=new HashMap<>();
        map.put("rows",obj.getResult());
        map.put("totals",obj.getTotal());
        return map;
    }

    //    选择客户时有效客户
    @RequestMapping("/have-type")
    @ResponseBody
    public Map<String,Object> haveType(Integer page, Integer size,String cusName){
        Page<Object> obj=PageHelper.startPage(page,size);
        service.haveType(cusName);

        Map<String,Object> map=new HashMap<>();
        map.put("rows",obj.getResult());
        map.put("total",obj.getTotal());
        return map;
    }

    //    设置关注
    @RequestMapping("/update-attention")
    @ResponseBody
    public void updateAttention(String attent){
        JSONObject obj= (JSONObject)JSONObject.parse(attent);
        Integer customId=obj.getObject("attent",Integer.class);
        System.out.println("关注："+customId);
        service.updateAttention(customId);
    }

    //    修改客户负责人
    @RequestMapping("/choose-emp")
    @ResponseBody
    public void updateEmp(Integer[] customIdArr, Integer employeesId) {
        service.updateEmp(customIdArr, employeesId);
    }

    //    查询销售部员工
    @RequestMapping("/select-emp")
    @ResponseBody
    public Map<String, Object> allEmp(Integer page, Integer size, String empName) {
        System.out.println(page + "," + size + "," + empName);
        Page<Object> obj = PageHelper.startPage(page, size);
        service.allEmp(empName);

        Map<String, Object> map = new HashMap<>();
        map.put("rows", obj.getResult());
        map.put("total", obj.getTotal());
        return map;
    }

    //    新增客户、联系人
    @MyLog(type = "客户",value = "新增了客户联系人")
    @RequestMapping("/save-customer")
    @ResponseBody
    public void saveCustomer(@RequestBody Customer customer) {
        service.saveCustomer(customer);
    }

//    客户导入
    @MyLog(type = "客户",value = "导入了客户")
    @RequestMapping("/dao-customer")
    @ResponseBody
    public String daoCustomer(String customer){
        return service.addCustomerInfo(customer);
    }

    //    删除客户
    @MyLog(type = "客户",value = "删除了客户")
    @RequestMapping("/del-customer")
    @ResponseBody
    public String cusDel(String customId) {
//        将接受到的String类型转换成JSONObject，再进行取值
        JSONObject obj = (JSONObject) JSONObject.parse(customId);
        Integer customIds = obj.getObject("customId", Integer.class);//取Integer值
        return service.cusDel(customIds);
    }

    //    查询客户类型
    @RequestMapping("/select-custype")
    @ResponseBody
    public List<CustomerType> selectCusType() {
        return service.selectCusType();
    }

//        查询客户姓名
    @RequestMapping("/select-cusname")
    @ResponseBody
    public List<Customer> selectCusName(String cusName) {
        System.err.println("新增的客户名称："+cusName);
        return service.selectCusName(cusName);
    }

    //    查询全部视图
    @RequestMapping("/view")
    @ResponseBody
    public List<Timeline> view(Integer customId){
        return service.view(customId);
    }

    //    根据客户id查询客户商机
    @RequestMapping("/select-business")
    @ResponseBody
    public List<BusinessInfo> selectBusiness(Integer customId) {
        return service.selectBusiness(customId);
    }

    //    根据客户id查询客户合同
    @RequestMapping("/select-contract")
    @ResponseBody
    public List<Contract> selectContract(Integer customId) {
        return service.selectContract(customId);
    }

    //    根据客户id查询订单
    @RequestMapping("/select-orderinfo")
    @ResponseBody
    public List<OrderInfo> selectOrderInfo(Integer customId){
        return service.selectOrderInfo(customId);
    }

    //    根据客户ID查询回款
    @RequestMapping("/select-ctschems")
    @ResponseBody
    public List<OrderInfo> selectCtschems(Integer customId){
        return service.selectCtschems(customId);
    }

    //    根据客户id查询客户关怀
    @RequestMapping("/select-customer-care")
    @ResponseBody
    public List<CustomerCare> selectCustomerCare(Integer customId){
        return service.selectCustomerCare(customId);
    }

    //    根据客户ID查询交付记录
    @RequestMapping("/select-deliverydetails")
    @ResponseBody
    public List<Deliverydetails> selectDeleveryDetails(Integer customId){
        return service.selectDeleveryDetails(customId);
    }

    //    根据客户ID查询维修单
    @RequestMapping("/select-maintain")
    @ResponseBody
    public List<Maintain> selectMaintain(Integer customId){
        return service.selectMaintain(customId);
    }

    //    根据客户id查询首要联系人
    @RequestMapping("/select-linkman")
    @ResponseBody
    public List<Culinkman> selectLinkman(Integer customId) {
        return service.selectLinkman(customId);
    }

    //    根据客户ID查询客户、联系人
    @RequestMapping("/customer-linkman")
    @ResponseBody
    public Customer customerLinkman(Integer customId) {
        return service.customerLinkman(customId);
    }

    //    客户合并
    @RequestMapping("/customer-merge")
    @ResponseBody
    public String merge(String id,Integer empId) {
        JSONObject object = (JSONObject) JSONObject.parse(id);
        Integer congId = object.getObject("congId", Integer.class);
        Integer daoId = object.getObject("daoId", Integer.class);
        System.out.println("controller客户："+congId+","+daoId);
        return service.merge(congId, daoId,empId);
    }

    //    合并成功，返回合并详细信息
    @RequestMapping("/customer-merge-xinxi")
    @ResponseBody
    public Map<Object,String> map(){
        return service.map();
    }

    //    查询合并记录
    @RequestMapping("/select-merge")
    @ResponseBody
    public List<MergeRecord> selectMerge(String str){
        String[] date=new String[2];
        String startTime="";
        String endTime="";
        if(str!=null && str!=""){
            date=str.split(",");
            startTime=date[0];
            endTime=date[1];
        }
        return service.selectMerge(startTime,endTime);
    }

    //    根据客户id查询客户地址
    @RequestMapping("/kehu-address")
    @ResponseBody
    public List<Receivingcontact> kehuAddress(Integer cusId) {
        return service.kehuAddress(cusId);
    }

    //    新增客户地址
    @RequestMapping("/add-address")
    @ResponseBody
    public void addAddress(String receiving) {
        System.out.println("receivingcontact:" + receiving);
//        将字符串转换成JSONObject
        JSONObject obj = (JSONObject) JSONObject.parse(receiving);
        Receivingcontact receivingcontact = obj.getObject("receivings", Receivingcontact.class);
        service.addAddress(receivingcontact);
    }

    //    删除客户地址
    @RequestMapping("/del-address")
    @ResponseBody
    public void delAddress(String receiving) {
        System.out.println("receiving:" + receiving);
//        将字符串转换成JSONObject
        JSONObject obj = (JSONObject) JSONObject.parse(receiving);
        Receivingcontact receivingcontact = obj.getObject("receivings", Receivingcontact.class);
        service.delAddress(receivingcontact);
    }

}